package com.example.papercut.controller;

import com.example.papercut.entity.PapercutTemplate;
import com.example.papercut.service.PapercutTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/templates/papercut")
public class PapercutTemplateController {

    private final PapercutTemplateService templateService;

    @Autowired
    public PapercutTemplateController(PapercutTemplateService templateService) {
        this.templateService = templateService;
    }

    /**
     * GET /api/templates/papercut/{id}
     * 1.2: 获取指定ID的模板详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<PapercutTemplate> getTemplateDetails(@PathVariable Integer id) {
        PapercutTemplate template = templateService.getTemplateDetails(id);
        if (template == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(template);
    }

    /**
     * GET /api/templates/papercut?categoryId=1&keyword=福&sortBy=rating
     * 1.1: 根据查询参数搜索模板（多维度筛选）
     */
    @GetMapping
    public ResponseEntity<List<PapercutTemplate>> searchTemplates(
        @RequestParam(required = false) Integer categoryId,
        @RequestParam(required = false) Integer themeId,
        @RequestParam(required = false) Integer techniqueId,
        @RequestParam(required = false) Integer layoutId,
        @RequestParam(required = false) Integer authorId,
        @RequestParam(required = false) String keyword,
        @RequestParam(required = false, defaultValue = "id") String sortBy) { // 默认按ID排序
        
        List<PapercutTemplate> templates = templateService.searchTemplates(
            categoryId, themeId, techniqueId, layoutId, authorId, keyword, sortBy
        );
        return ResponseEntity.ok(templates);
    }
}